package zozopo.manual;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import zozopo.comm.Validate;
import zozopo.database.BusiDatabaseManager;
import zozopo.manual.operations.ReleaseOS;
import zozopo.manual.operations.ReleaseTB;
import zozopo.operations.comm.DictionaryOS;
import zozopo.operations.comm.DictionaryTB;

import zozopo.user.operations.UserTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class RecommendAction extends ActionSupport implements Action ,SessionAware,ServletRequestAware{
	private int id;
	private String subject;
	private String lable;
	private String url;
	private String brand;
	private String valicode; 
	private String context;
	public String getContext() {
		return context;
	}

	public void setContext(String context) {
		this.context = context;
	}
	private List<DictionaryTB> lableList;
	
	public List<DictionaryTB> getLableList() {
		return lableList;
	}

	public void setLableList(List<DictionaryTB> lableList) {
		this.lableList = lableList;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	HttpServletRequest request;

	private Map<Object, Object> session;
	
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	
	public String execute(){
		UserTB user = (UserTB) session.get("userInfo");
		
		if(user == null){
			return NONE;
		}
		
		Character limitflag4 = user.getUser_limitflag().charAt(4);
		Integer intLimitflag4 = Integer.parseInt(limitflag4.toString());
		
		if(intLimitflag4 <= 0){
			return NONE;
		}
		
		BusiDatabaseManager databaseConnection = new BusiDatabaseManager();
		Connection conn = databaseConnection.getConnection();
		
		ReleaseOS releaseOS = new ReleaseOS(conn);
		ReleaseTB release = new ReleaseTB();
		
		release.setRelease_subject(subject);
		release.setRelease_lable(lable);
		release.setRelease_brand(brand);
		release.setRelease_secondbrand("");
		release.setRelease_url(url);
		
		context = context.replaceAll("'", "\\\\'");
		context = Validate.htmlLabelFilter(context);
		
		release.setRelease_context(context);
		release.setRelease_from("REMOTE");
		release.setRelease_kind("KIND.RECOMMENDSTORE");
		release.setRelease_useremail(user.getUser_email());
		release.setRelease_ip(getIpAddr(request));
		
		
		
		
		if(!releaseOS.insertRelease(release)){
			databaseConnection.releaseConnection(conn);
			return ERROR;
		}
		id = releaseOS.getMaxIdByEmail(user.getUser_email());
		
		
		//databaseConnection.releaseConnection(conn);
		

		
		
		return SUCCESS;
	}
	


	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getLable() {
		return lable;
	}

	public void setLable(String lable) {
		this.lable = lable;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public String getValicode() {
		return valicode;
	}

	public void setValicode(String valicode) {
		this.valicode = valicode;
	}

	public void validate() {
		// TODO Auto-generated method stub
		 
		if(subject == null || subject.trim().equals("")){
			addFieldError("errormessage", "网店名称不能为空");
			return;
		}
		
		if(Validate.isSpecialCharacters(subject)){
			addFieldError("errormessage", "标题长度不能包含特殊字符 ;\"'\\`|$");
			return;
		}
		
		if(url == null || url.trim().equals("")){
			addFieldError("errormessage", "网店连接不能为空");
			return;
		}
		
		if(brand == null || brand.trim().equals("")){
			addFieldError("errormessage", "经营品牌不能为空");
			return;
		}
		
		if(Validate.isSpecialCharacters(brand)){
			addFieldError("errormessage", "品牌长度不能包含特殊字符  ;\"'\\`|$");
			return;
		}
		
		if(lable == null || lable.trim().equals("")){
			lable="MANUAL.LABLE.OTHER";
		}
		ReleaseOS releaseOS = new ReleaseOS();
		
		if(releaseOS.isExistUrl(url)){
			addFieldError("errormessage", "URL已经存在不可以发重复URL连接!");
			releaseOS.releaseConnection();
			return;
		}
		releaseOS.releaseConnection();
	}
	
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request = arg0;
		
	}
	
	/**
     * 获得客户端真是ip
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
            ip = request.getHeader("Proxy-Client-IP");    
        }    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
            ip = request.getHeader("WL-Proxy-Client-IP");    
        }    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
            ip = request.getRemoteAddr();    
     }
		return ip; 
    }
}
